; kernel/kernel.asm --> Kernel
; This is loaded from the floppy drive by NTLOAD.BIN, as KERN86.BIN.
; First we have the system call vectors, which start at a static point
; for programs to use. Following that is the main kernel code and
; then additional system call code is included.
; ==================================================================
; This was written by Mike Saunders and his great team. I modified it 
; by removing certain functions like the TUI, and added a cool looking
; CLI + So-called GUI.

	BITS 16
	; We are loaded at 0x2000:0x0000
	org 0
	%DEFINE POS_VER '1.07.2'	; OS version number
	%DEFINE POS_API_VER 18	; API version for programs to check
	cpu 586 ; Default to 586
	
	; This is the location in RAM for kernel disk operations, 24K
	; after the point where the kernel has loaded; it's 8K in size,
	; because external programs load after it at the 32K point:

	disk_buffer	equ	0x6000


; ------------------------------------------------------------------
; OS CALL VECTORS -- Static locations for system call vectors
; Note: these cannot be moved, or it'll break the calls!

; The comments show exact locations of instructions in this section,
; and are used in programs/mikedev.inc so that an external program can
; use a MikeOS system call without having to know its exact position
; in the kernel source code...
os_call_vectors:
	; Probably I don't want to mess up the syscalls but I would as things 
	; get mature,
	jmp init			; 0000h -- Called from NTLOAD.BIN
	; We jump to the initialization at runtime
	; because we don't want to run all the calls as things would get
	; messed up
	; Here's all accessible to the applications
	; MikeOS Syscalls
	jmp os_print_string		; 0003h
	jmp os_move_cursor		; 0006h
	jmp os_clear_screen		; 0009h
	jmp os_print_horiz_line		; 000Ch
	jmp os_print_newline		; 000Fh
	jmp os_wait_for_key		; 0012h
	jmp os_check_for_key		; 0015h
	jmp os_int_to_string		; 0018h
	jmp os_speaker_tone		; 001Bh
	jmp os_speaker_off		; 001Eh
	jmp os_load_file		; 0021h
	jmp os_pause			; 0024h
	jmp os_fatal_error		; 0027h
	jmp os_draw_background		; 002Ah
	jmp os_string_length		; 002Dh
	jmp os_string_uppercase		; 0030h
	jmp os_string_lowercase		; 0033h
	jmp os_input_string		; 0036h
	jmp os_string_copy		; 0039h
	jmp os_dialog_box		; 003Ch
	jmp os_string_join		; 003Fh
	jmp os_get_file_list		; 0042h
	jmp os_string_compare		; 0045h
	jmp os_string_chomp		; 0048h
	jmp os_string_strip		; 004Bh
	jmp os_string_truncate		; 004Eh
	jmp os_bcd_to_int		; 0051h
	jmp os_get_time_string		; 0054h
	jmp os_get_api_version		; 0057h
	jmp os_file_selector		; 005Ah
	jmp os_get_date_string		; 005Dh
	jmp os_send_via_serial		; 0060h
	jmp os_get_via_serial		; 0063h
	jmp os_find_char_in_string	; 0066h
	jmp os_get_cursor_pos		; 0069h
	jmp os_print_space		; 006Ch
	jmp os_dump_string		; 006Fh
	jmp os_print_digit		; 0072h
	jmp os_print_1hex		; 0075h
	jmp os_print_2hex		; 0078h
	jmp os_print_4hex		; 007Bh
	jmp os_long_int_to_string	; 007Eh
	jmp os_long_int_negate		; 0081h
	jmp os_set_time_fmt		; 0084h
	jmp os_set_date_fmt		; 0087h
	jmp os_show_cursor		; 008Ah
	jmp os_hide_cursor		; 008Dh
	jmp os_dump_registers		; 0090h
	jmp os_string_strincmp		; 0093h
	jmp os_write_file		; 0096h
	jmp os_file_exists		; 0099h
	jmp os_create_file		; 009Ch
	jmp os_remove_file		; 009Fh
	jmp os_rename_file		; 00A2h
	jmp os_get_file_size		; 00A5h
	jmp os_input_dialog		; 00A8h
	jmp os_list_dialog		; 00ABh
	jmp os_string_reverse		; 00AEh
	jmp os_string_to_int		; 00B1h
	jmp os_draw_block		; 00B4h
	jmp os_get_random		; 00B7h
	jmp os_string_charchange	; 00BAh
	jmp os_serial_port_enable	; 00BDh
	jmp os_sint_to_string		; 00C0h
	jmp os_string_parse		; 00C3h
	jmp os_run_basic		; 00C6h
	jmp os_port_byte_out		; 00C9h
	jmp os_port_byte_in		; 00CCh
	jmp os_string_tokenize		; 00CFh
	; POSx86 Syscalls (Useless)
	jmp disable_root ; 00D2h
	jmp enable_root ; 00D5h
	jmp os_filemgr ; 00D8h
	; Except these
	jmp os_print_string_with_color ; 00DBh
	jmp ext_load_file ; 00DEh'
	jmp set_int20_mode ; 00E1h
	jmp os_pcgui_switch ; 00E4h
	jmp switch_basic ; 00E7h
; This is the end of system calls
; In MikeOS,Tachyon,POS,etc.
; Memory Segmentation is as follows
; +++++++++++++++
; +++++++++++++++
; KERNEL            ----> This takes 24K in RAM
; +++++++++++++++
; +++++++++++++++
; ---------------
; DISK BUFFER       ----> This takes 8K in RAM	
; ---------------
; So for example, our system call print_string is located at 0x0003 in RAM. 
; When a program loads it loads at 32768 since the point before 32768 is taken 
; by the disk buffer. 
; That means our program is somewhere at 32K in the segment, now when 
; we write os_print_string, it's basically call 0003h, we actually jump 
; to this hex location, and then return back.
; ------------------------------------------------------------------
; START OF MAIN KERNEL CODE

init:
	call init_sys ; Initialize the System with the chosen RAM + Check
	; the correct boot device
no_change:
	call os_seed_random		; Seed random number generator
	; Install Required Interrupts
	; For MS-DOS Stuff.
	mov cx,21h
	mov si,int21_ivt
	call SET_INT
	; For exiting Applications
	mov cx,20h
	mov si,int20_ivt
	call SET_INT
	; Let's see if there's a file called AUTORUN.PEX and execute
	; it if so, before going to the program launcher menu
run_stuff:
	mov ax, autorun_bin_file_name
	call os_file_exists
	jc os_fatal_error		; Skip next three lines if AUTORUN.PEX doesn't exist

	mov cx, 32768		; Otherwise load the program into RAM...
	call os_load_file
	jmp execute_bin_program		; ...and move on to the executing part
	
no_autorun_bin:
	;mov ax,1003h
	;mov bx,0
	;int 0x10
	call os_command_line; And go to the app selector menu when BASIC ends
execute_bin_program:
        call os_clear_screen                ; Clear screen before running

        mov ax, 0                        ; Clear all registers
        mov bx, 0
        mov cx, 0
        mov dx, 0
        mov si, 0
        mov di, 0

        call 0x8000
		; Call the external program code,
       ; loaded at second 32K of segment
	   ; Switch Video Modes (80x25 --> 80x50)
		xor bl,bl
		mov ax,0x1112
		int 0x10
		; Set VGA 400 lines
	mov ax,1202h 
       mov bl,30h 
        int 10h 
		
	
		jmp os_command_line ; QEMU SHIT!
		lol1 db 'LOL',0
        ;jmp option_screen        ; and go back to the program list
		;in: es:di=4k buffer
		;out: buffer filled with font
		
; ------------------------------------------------------------------
; SYSTEM VARIABLES -- Settings for programs and system calls

	; Time and date formatting

	fmt_12_24	db 0		; Non-zero = 24-hr format

	fmt_date	db 1, '/'	; 0, 1, 2 = M/D/Y, D/M/Y or Y/M/D
					; Bit 7 = use name for months
					; If bit 7 = 0, second byte = separator character
	
; ------------------------------------------------------------------
; drivers -- Code to pull into the kernel
; This is what makes our kernel around 14KB :)
	; OS Related Stuff (Like CLI,PCGUI Etc.)
	%include "drivers/CLI/cli.asm" ; PCGUI Command Line Interface
	; Disk Routines
	%include "drivers/FAT12/extdsk.asm" ; EXT Floppy Read File
	%include "drivers/FAT12/extload.asm" ; ---> EXT Floppy Load File
 	%include "drivers/FAT12/fat12.asm" ; FAT12 Mini Driver
	; Screen/IO
	%include "drivers/IO/ports.asm" ; Serial Ports
	%include "drivers/IO/screen.asm" ; Screen/IO Routines
	%include "drivers/IO/sound.asm" ; Sounds
	%include "drivers/IO/string.asm" ; String Manipulation Routines 
	%include "drivers/IO/keyboard.asm" ; Input and other Keyboard routines
	; MPU
	%include "drivers/MPU/math.asm" ; MPU
	%include "drivers/misc.asm" ; Miscellaneous 
	; Initialization
	%include "drivers/init.asm"
	; VGA
	%include "drivers/VGA/graphic.asm"
	;%include "drivers/VGA/font.fnt"
	;%include "drivers/VGA/8050.fnt"
	; Devices
	%include "drivers/NET/network.asm" ;---> Awesome!
	; Really Useless
	%include "drivers/program.asm" ; Useless
	%include "drivers/security.asm" ; Useless
	; MACRO's to make life easier
	%include "drivers/MACRO/macro.asm"
	; Kernel Panic :{
	%include "drivers/PANIC/panic.asm"
	; Some DOS Stuff
	%include "drivers/int21/int21.asm"
	; Data
	%include "data.asm" ; ---> Data like strings
	
; ==================================================================
; END OF KERNEL
; ==================================================================

